package tank;

import java.awt.*;
import java.util.Random;

import static tank.Dir.UP;

/**
 * @author kc
 * @date 2021/4/22 0022 0:11
 */

public class Tank {
    public static int WIDTH = ResourceMgr.goodTankU.getWidth();
    public static int HEIGHT = ResourceMgr.goodTankU.getHeight();

    private int x;
    private int y;
    private Dir dir = UP;
    private final static int SPEED = PropertyMgr.getInt("tankSpeed");;
    private boolean living = true;
    private Group group;

    private Random random = new Random();

    private boolean moving = true;

    private TankFrame tf;

    private Rectangle rectangle = new Rectangle();

    public Tank() {
    }

    public Tank(int x, int y, Dir dir, Group group, Boolean moving, TankFrame tf) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tf = tf;
        this.group = group;
        this.moving = moving;

        rectangle.x = this.x;
        rectangle.y = this.y;
        rectangle.width = WIDTH;
        rectangle.height = HEIGHT;
    }

    private static void run() {
        new Audio("audio/tank_fire.wav").play();
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void paint(Graphics graphics) {
        if (!living) {
            tf.tanks.remove(this);
        }

        switch (dir) {
            case LEFT:
                graphics.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankL : ResourceMgr.badTankOneL, x, y, null);
                break;
            case UP:
                graphics.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankU : ResourceMgr.badTankOneU, x, y, null);
                break;
            case RIGHT:
                graphics.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankR : ResourceMgr.badTankOneR, x, y, null);
                break;
            case DOWN:
                graphics.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankD : ResourceMgr.badTankOneD, x, y, null);
                break;
            default:
                break;
        }


        move();
    }

    private void move() {
        if (!moving) {
            return;
        }
        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
            default:
                break;
        }
        if (this.group == Group.BAD && random.nextInt(100) > 90) {
            this.fire();
        }
        if (this.group == Group.BAD && random.nextInt(100) > 95) {
            randomDir();
        }
        boundsCheck();
        rectangle.x = this.x;
        rectangle.y = this.y;
    }

    private void boundsCheck() {
        boolean isCheck = false;
        if (this.x < 5) {
            x = 5;
            isCheck =true;
        }
        if (this.y < 25) {
            y = 25;
            isCheck =true;
        }
        if (this.x > TankFrame.WIDTH - Tank.WIDTH - 5) {
            this.x = TankFrame.WIDTH - Tank.WIDTH - 5;
            isCheck =true;
        }
        if (this.y > TankFrame.HEIGHT - Tank.HEIGHT - 5) {
            this.y = TankFrame.HEIGHT - Tank.HEIGHT - 5;
            isCheck =true;
        }
        if (this.group == Group.BAD && isCheck){
            randomDir();
        }

    }

    private void randomDir() {
        this.dir = Dir.values()[random.nextInt(4)];
    }


    public void fire() {
        int bx = this.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
        int dy = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
        tf.bullets.add(new Bullet(bx, dy, this.dir, this.group, tf));
        if (this.group == Group.GOOD) {
            new Thread(Tank::run).start();
        }
    }

    public void die() {
        this.living = false;
    }

    public Rectangle getRectangle() {
        return rectangle;
    }

    public void setRectangle(Rectangle rectangle) {
        this.rectangle = rectangle;
    }
}
